# 这个文件用于测试Trader的稳健性
from ITrader import ITrader
from core.Logger import Logger
from core.PublicContext import PublicContext
from common.Order import Order
from common.OrderUpdate import OrderUpdate
import json
import time

portfolio = {'TestStrategy': {'600600': {'sellable_position': 200,
                                         'unsellable_position': 0,
                                         'locked_position': 0},
                              '600030': {'sellable_position': 1000,
                                         'unsellable_position': 0,
                                         'locked_position': 0},
                              'cash': 10000},
             'NoneStrategy': {'300014': {'sellable_position': 100,
                                         'unsellable_position': 0,
                                         'locked_position': 0},
                              '600030': {'sellable_position': 1000,
                                         'unsellable_position': 0,
                                         'locked_position': 0},
                              'cash': 8000},
             }


# 开始测试
logger = Logger()
logger.start()

public_context = PublicContext()
public_context.logger = logger


trader = ITrader()
trader.public_context = public_context
trader.initialize(json.dumps(portfolio))
trader.start()

print(trader.portfolio)
trader.print_orders()


time.sleep(3)
print('下无效买单')
order = Order('TestStrategy', 'STG_ORDER_1', 'market_order',
              '600600', 'BUY', 108, 100, 'test1')
print(trader.send_order(order))
time.sleep(1)
print(trader.portfolio)
trader.print_orders()

print('下无效卖单')
order = Order('TestStrategy', 'STG_ORDER_1', 'market_order',
              '600600', 'SELL', 108, 300, 'test1')
print(trader.send_order(order))
time.sleep(1)
print(trader.portfolio)
trader.print_orders()


print('下有效买单')
order = Order('TestStrategy', 'STG_ORDER_1', 'limit_order',
              '600600', 'BUY', 40, 200, 'test1')
print(trader.send_order(order))
time.sleep(1)
print(trader.portfolio)
trader.print_orders()


print('下有效卖单')
order = Order('TestStrategy', 'STG_ORDER_2', 'market_order',
              '600600', 'SELL', 88, 100, 'test1')
print(trader.send_order(order))
time.sleep(1)
print(trader.portfolio)
trader.print_orders()


print('买单部分成交')
order_update = OrderUpdate('', 'SYS_ORDER_1', 'DEAL', '600600', 'BUY', 100, 36)
trader.put_event('order_update', order_update)
time.sleep(1)
print(trader.portfolio)
trader.print_orders()


print('卖单全部成交')
order_update = OrderUpdate('', 'SYS_ORDER_2', 'DEAL', '600600', 'SELL', 100, 90)
trader.put_event('order_update', order_update)
time.sleep(1)
print(trader.portfolio)
trader.print_orders()


print('买单撤单')
order_update = OrderUpdate('', 'SYS_ORDER_1', 'CANCEL', '600600', '', 0, 0)
trader.put_event('order_update', order_update)
time.sleep(1)
print(trader.portfolio)
trader.print_orders()

time.sleep(3)
logger.log_message('SYSTEM_EXIT')
trader.trader_queue.put('SYSTEM_EXIT')
print('over')






